//
//  RBZoneChooseVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/9/7.
//

#import "RBZoneChooseVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import <UIControl+YYAdd.h>
#import "RBResponse.h"
#import <YYModel.h>

@interface RBProCityZoneChooseModel : RBResponse

@property(nonatomic, copy)NSString *id;
@property(nonatomic, copy)NSString *title;
@property(nonatomic, strong)NSArray *data;

@end

@implementation RBProCityZoneChooseModel

+ (NSDictionary *)rbRenameKeyDic
{
    return @{
        @"title":@[@"name"],
        @"data":@[@"children"],
    };
}

+ (NSDictionary *)rbAssignClassDic
{
    return @{
        @"data":[RBProCityZoneChooseModel class]
    };
}

@end

@interface RBZoneChooseVC ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property(nonatomic, strong)IBOutlet UIPickerView *pickerView;
@property(nonatomic, strong)NSArray *dataSource;

@property(nonatomic, assign)NSInteger provinceIndex;
@property(nonatomic, assign)NSInteger cityIndex;
@property(nonatomic, assign)NSInteger zonIndex;

@end

@implementation RBZoneChooseVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    [self.view addSubview:self.contentView];
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = self.contentView.height + kRBSafeBottomHeight;
    self.contentView.left = 0;
    self.contentView.top = kRBMainScreenHeight;
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"city.json" withExtension:@""]];
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions|NSJSONWritingPrettyPrinted error:nil];
    self.dataSource =
    [NSArray yy_modelArrayWithClass:RBProCityZoneChooseModel.class json:dataArray];
    [self.pickerView reloadAllComponents];
}

#pragma mark - Public

- (void)show
{
    [UIView animateWithDuration:0.05 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.contentView.bottom = kRBMainScreenHeight;
        }];
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.15 animations:^{
        self.contentView.top = kRBMainScreenHeight;
    }];
    [UIView animateWithDuration:0.05 delay:0.1 options:0 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - UIPickerViewDataSource & Delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.dataSource.count? 3: 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return self.dataSource.count;
    }
    else if(component == 1)
    {
        RBProCityZoneChooseModel *provinceModel = self.dataSource[self.provinceIndex];
        return provinceModel.data.count;
    }
    else
    {
        RBProCityZoneChooseModel *provinceModel = self.dataSource[self.provinceIndex];
        RBProCityZoneChooseModel *cityModel = provinceModel.data[self.cityIndex];
        return cityModel.data.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = @"";
    if(component == 0)
    {
        RBProCityZoneChooseModel *provinceModel = self.dataSource[row];
        title = provinceModel.title;
    }
    else if(component == 1)
    {
        RBProCityZoneChooseModel *provinceModel = self.dataSource[self.provinceIndex];
        RBProCityZoneChooseModel *cityModel = provinceModel.data[row];
        title = cityModel.title;
    }
    else
    {
        RBProCityZoneChooseModel *provinceModel = self.dataSource[self.provinceIndex];
        RBProCityZoneChooseModel *cityModel = provinceModel.data[self.cityIndex];
        RBProCityZoneChooseModel *zoneModel = cityModel.data[row];
        title = zoneModel.title;
    }
    return title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 2)
    {
        self.zonIndex = row;
    }
    else if(component == 1)
    {
        self.cityIndex = row;
        self.zonIndex = 0;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:NO];
    }
    else
    {
        self.provinceIndex = row;
        self.cityIndex = 0;
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:NO];
        self.zonIndex = 0;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:NO];
    }
}

#pragma mark - Event

- (IBAction)cancelBtnTapped:(id)sender
{
    [self dismiss];
}

- (IBAction)confirmBtnTapped:(id)sender
{
    NSLog(@"%zd-%zd-%zd", self.provinceIndex, self.cityIndex, self.zonIndex);
    RBProCityZoneChooseModel *pro = self.dataSource[self.provinceIndex];
    RBProCityZoneChooseModel *city = pro.data[self.cityIndex];
    RBProCityZoneChooseModel *zone = city.data[self.zonIndex];
    NSLog(@"%@-%@-%@", pro.title, city.title, zone.title);
    if(self.block)
    {
        self.block(pro.title, city.title, zone.title);
    }
    [self dismiss];
}

#pragma mark - Getter

@end
